<?php
namespace App\Http\Controllers;


use App\Exceptions\BasicException;

class BaseController extends Controller
{
    protected $drawType = '';
    //抽奖显示大礼物
    public function draw_show_price($price, $check_price = 52000)
    {
        $is_show_price = false;
        if ($price >= $check_price) {
            $is_show_price = true;
        }
        return $is_show_price;
    }

    public function forDrawGift($drawData, $totalCoin = 0)
    {
        $config_key = strtoupper($this->drawType).'_GUARANTEED';
        $guaranteed_data = explode(',', app('model_config')->getValue($config_key));
        $check_gift_price = $guaranteed_data[1] ?? 0;
        $is_guaranteed = true;
        $redis = app('redis')->connection('game');
        $redis_key = $this->drawType.':guaranteed';
        $guaranteed_count = 0;

        $game_user = $drawData['draw_data']['user'];
        $appoint_gift_info = $drawData['supInfo']['appoint_gift_info'] ?? [];
        $appoint_type = $appoint_gift_info['type'] ?? 0;
        if (empty($guaranteed_data) || empty($check_gift_price) || $drawData['draw_data']['tag'] != 'public' || $game_user->status == 0) {
            $is_guaranteed = false;
        }
        if ($appoint_type == 2) {//本次抽奖有保底补偿
            $redis->hset($redis_key, UID, 0);
            $is_guaranteed = false;
        }
        $is_show_price = $is_show_animation = false;
        foreach ($drawData['gift_list'] as &$draw) {
            if (!$is_show_price) {
                $is_show_price = $this->draw_show_price($draw['price']);
            }
            if (!$is_show_animation) {
                $is_show_animation = $this->draw_show_price($draw['price'], $drawData['poolConfig']['big_gift_coin']);
            }
            $totalCoin  += $draw["price"] * $draw["count"];
            if ($is_guaranteed) {
                if ($draw['price'] >= $check_gift_price) {
                    $redis->hset($redis_key, UID, 0);
                    $is_guaranteed = false;
                } else if ($draw['price'] < $check_gift_price) {
                    $guaranteed_count = $redis->hincrby($redis_key, UID, $draw['count']);
                }

            }
        }
        if ($guaranteed_count > 0) {
            $game_user->guaranteed_count = $guaranteed_count;
            $game_user->save();
        }
        return [
            'drawData' => $drawData,
            'totalCoin' => $totalCoin,
            'is_show_price' => $is_show_price,
            'is_show_animation' => $is_show_animation,
        ];
    }

    // todo 临时验证(解决ios未验证版本问题
    public function checkVersion($client, $version)
    {
        return true;
    }

}



?>
